﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using WPFExcelReport.Helpers;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Windows.Input;
namespace WPFExcelReport
{
    public partial class TVA31 : UIVAKDBEZ
    {

   
        public TVA31()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAKDBEZ();
            this.DataContext = MV;
            MESSAGES += "VAKDCHK;VAKDFARBECHG;";
            InitializeComponent();
            ART = "Y"; 
         }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);//
            dwVAK = dwMaster;
            if (dwMaster == null)
            {
                KEY = "";
                TXTISIND = "";
            }
            else
            {
                KEY = dwMaster["VAKBKOLL"].ToString() + dwMaster["VAKBART"].ToString() + dwMaster["VAKBFARBE"].ToString() + dwMaster["VAKBKUNDNR"].ToString().Trim().PadRight(8);
                CR = dwMaster["CR"].ToString();
                CG = dwMaster["CG"].ToString();
                CB = dwMaster["CB"].ToString();
                CGRP = dwMaster["CGRP"].ToString();
                VAFBEZ = dwMaster["VAFBEZ"].ToString();
                TXTISIND = (dwMaster["VAKBKOLL"].ToString() + dwMaster["VAKBART"].ToString() + dwMaster["VAKBKUNDNR"].ToString().Trim().PadRight(8) + dwMaster["VAKBFARBE"].ToString()).PadRight(60);

            }
        }
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            try
            {
                base.OnWorkflowIDChange(e);
                MessageInfo msg = e.Info;
                string task = msg.MSGTYPE;
                switch (task)
                {
                    case "VAKDCHK":
                        // VAKDCHK(msg);
                        break;
                    case "VAKDFARBECHG":
                        VAKDFARBECHG(msg);
                        break;

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void VAKDCHK(MessageInfo msg)
        {
            if (dwMaster == null)
                return;
            dwData = TBMain.NewRow();
            dwData["VAKBMANDANT"] = app.Mandant;
            dwData["VAKBKOLL"] = msg.KOLL;
            dwData["VAKBART"] = msg.ART;
            dwData["VAKBFARBE"] = msg.FARBE;
            dwData["VAKBKUNDNR"] = msg.P1;
            SResult = ((VVAKDBEZ)MV).QueryVAKDBEZ(dwData, 5);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
        }

        public void VAKDFARBECHG(MessageInfo msg)
        {
            dwData = TBMain.NewRow();
            dwData["VAKBMANDANT"] = app.Mandant;
            dwData["VAKBKOLL"] = msg.KOLL;
            dwData["VAKBART"] = msg.ART;
            dwData["VAKBFARBE"] = " AND VAKBFARBE  = '" + msg.P2 + "'";
            dwData["VAKBKUNDNR"] = msg.P1;
            
            SResult = TMV.QVAK05(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }

        }

        public string currentkey
        {
            get
            {
                if (dwMaster == null) return "";
                return dwMaster["VAKBKOLL"].ToString() + dwMaster["VAKBART"].ToString() + dwMaster["VAKBFARBE"].ToString() + dwMaster["VAKBKUNDNR"].ToString();
            }
        }
        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != currentkey)
                return;
            if (KEY.Length < 47)
                return;
            dwData = TBMain.NewRow();
            dwData["VAKBMANDANT"] = app.Mandant;
            dwData["VAKBKOLL"] = ORDERNUM.Substring(0, 5);
            dwData["VAKBART"] = ORDERNUM.Substring(5, 24);
            dwData["VAKBFARBE"] = " AND VAKBFARBE  = '" + ORDERNUM.Substring(29, 10) + "'";

            dwData["VAKBKUNDNR"] = ORDERNUM.Substring(39, 8);
            SResult = TMV.QVAK05(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            IsGSSave = true;
            IsEdit = false;
            IsSaved = false;
            base.QueryCommand(sender, e);
            dwData = TBMain.NewRow();
            dwData["VAKBMANDANT"] = app.Mandant;
            dwData["VAKBKOLL"] = KOLL;
            dwData["VAKBART"] = ART;
            dwData["VAKBFARBE"] = " AND VAKBFARBE  LIKE '%" + FARBE + "%'";
            dwData["VAKBKUNDNR"] = KUNDNR;
            dwData["VAKBKDFARBNR"] = KDFARBNR;
            dwData["VAKBKDARTBEZ"] = DES;
            SResult = TMV.QVAK05(dwData);
            MergeData(SResult, true);

        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            IsGSSave = false;
            KOLL = "XY ";
            try
            {
                if (!IsNew)
                {
                    IsKDFound = true;
                    KOLL = "XY ";
                    //MessageBox.Show("请先初始化色号");
                    IsNew = true;
                    return;
                }
                if (KOLL.Trim().Length < 2)
                {
                    MessageBox.Show("请选择季节");
                    return;
                }
                if (FARBE.EndsWith("*") || FARBE.Trim().Length < 2)
                {
                    MessageBox.Show("请初始化色号");
                    return;
                }
                if (KUNDNR.Trim().Length < 6)
                {
                    MessageBox.Show("请选择客户");
                    return;
                }

                PM["KOLL"] = KOLL;
                PM["ART"] = Helpers.Helper.Left(FARBE.PadRight(24), 24);
                PM["FARBE"] = FARBE;
                PM["KUNDNR"] = KUNDNR;

                TMV.NEWVAKDBEZ(PM);//到这儿都理解
                vwMaster.View.MoveCurrentToFirst();
                if (dwVAK != null)
                {
                    dwVAK["VAFFARBGRP"] = FARBGRP;
                    dwVAK["CGRP"] = CGRP;
                    dwVAK["VAKBKDFARBNR"] = KDFARBNR;
                    dwVAK["VAFBEZ"] = DES;
                    dwVAK["KDKUNDNA"] = KDKUNDNA;

                }
                base.NewCommand(sender, e);
                FARBE = "";
                KUNDNR = "";
                cSKD.vwMaster.View.MoveCurrentTo(-1);
                cTSSD0561.vwMaster.View.MoveCurrentTo(-1);
                //cSVA02.vwMaster.View.MoveCurrentTo(-1);
                IsNew = false;
                IsEdit = false;
                ISAVE = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
          
            base.EditCommand(sender, e);
        }

        public void QUERY_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Focus();
                QueryCommand(null, null);
            }

        }

        private void QueryXJCommand(object sender, RoutedEventArgs e)
        {
            KUNDNR = "";
            KOLL = "";
            KDFARBNR = "";
            FARBE = "";
            DES = "";
            VICC_WFEVENTS.SVWF.XJ(RUNID);
        }

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            IsGSSave = false; 
            if (!IsCopy)
            {
                IsCopy = true;
                KUNDNR = "";
                return;
            }
            if (KUNDNR.Length == 0)
            {
                MessageBox.Show("请选择客户");
                return;
            }
            if (dwMaster == null)
                return;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["VAKBFARBE"] = ".".PadRight(10);// Helper.Left(dwData["VAKBART"].ToString().PadRight(10), 10);
            dwData["VAKBKUNDNR"] = Helpers.Helper.Left(KUNDNR.PadRight(8), 8);
            TMV.Insert(dwData);
            KEY = "";
            vwMaster.View.MoveCurrentToFirst();
            IsCopy = false;
            KUNDNR = "";
            base.CopyCommand(sender, e);
            cKD02.vwMaster.View.MoveCurrentTo(-1);
            IsEdit = false;
            ISAVE = false;
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            try
            { 
                if (cDEL.Text.ToString().Trim().ToUpper() != "Y")
                {
                    MessageBox.Show("请设定删除标志位Y");
                    return;
                }

                if (dwVAK == null)
                    return;
                string color = dwVAK["VAKBFARBE"].ToString().Trim();
                TMV.Delete(dwVAK);
                TMV.DeleteCOLOR(color);
                cDEL.Text = "N";
                if (vwMaster.View != null)
                {
                    vwMaster.View.MoveCurrentTo(-1);
                }

                base.DeleteCommand(sender, e);
                ISAVE = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);
        }

        public override void OnCGRPChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwVAK == null)
                return;
            dwVAK["CGRP"] = CGRP;
            base.OnCGRPChg(value);
        }

    }
}
